package com.dge.app.utils;

import com.dge.app.ResourceTable;
import ohos.agp.components.AttrSet;
import ohos.agp.text.Font;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

public class ResourceUtils {
    public static HashMap<String, Integer> nameResourceMap = new HashMap();

    static {
        nameResourceMap.put("一场仗", ResourceTable.Media_ic_a_battle);
        nameResourceMap.put("比学赶超", ResourceTable.Media_ic_competition);
        nameResourceMap.put("超时亮灯", ResourceTable.Media_ic_timeout_light);
        nameResourceMap.put("成果晾晒", ResourceTable.Media_ic_result_show);
        nameResourceMap.put("督办圈阅", ResourceTable.Media_ic_supervise_and_read);
        nameResourceMap.put("揭榜挂帅", ResourceTable.Media_ic_rank);
        nameResourceMap.put("目标任务", ResourceTable.Media_ic_target_task);
        nameResourceMap.put("一表通", ResourceTable.Media_ic_sheet);
        nameResourceMap.put("重点项目", ResourceTable.Media_ic_important_project);
        nameResourceMap.put("目标计划", ResourceTable.Media_ic_fun_ji);
        nameResourceMap.put("部门账簿", ResourceTable.Media_ic_fun_bu);
        nameResourceMap.put("流通备案", ResourceTable.Media_ic_fun_bei);
        nameResourceMap.put("共享超市", ResourceTable.Media_ic_fun_xiang);
        nameResourceMap.put("开放超市", ResourceTable.Media_ic_fun_fang);
        nameResourceMap.put("流通考核", ResourceTable.Media_ic_fun_kao);
        nameResourceMap.put("资源画像", ResourceTable.Media_ic_fun_hua);
        nameResourceMap.put("资源云行", ResourceTable.Media_ic_fun_yun);
        nameResourceMap.put("更多", ResourceTable.Media_ic_fun_more);
    }

    // /**
    //  * 获取字体
    //  *
    //  * @param context 上下文
    //  * @param attrSet 属性集
    //  * @param name 属性name
    //  * @return 返回Font
    //  */
    // public static Font getFont(Context context, AttrSet attrSet, String name) {
    //     String fileName = getString(attrSet, name, "");
    //     return getFontFromRawFile(context, fileName);
    // }

    /**
     * 从resources/rawfile 读取字体文件
     *
     * @param context  上下文
     * @param fileName 字体文件名称
     * @return Font
     */
    public static Font getFontFromRawFile(Context context, String fileName) {
        String path = "resources/rawfile/" + fileName;
        ResourceManager resManager = context.getResourceManager();
        RawFileEntry rawFileEntry = resManager.getRawFileEntry(path);
        Resource resource = null;
        try {
            resource = rawFileEntry.openRawFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (resource != null) {
            File file = new File(context.getCacheDir(), fileName);
            OutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(file);
                int index;
                byte[] bytes = new byte[1024];
                while ((index = resource.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, index);
                    outputStream.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    resource.close();
                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Font.Builder builder = new Font.Builder(file);
            return builder.build();
        }
        return null;
    }

    // /**
    //  * 获取Element
    //  *
    //  * @param attrSet 属性集
    //  * @param name 属性name
    //  * @return 返回Element
    //  */
    // public static Element getElement(AttrSet attrSet, String name) {
    //     if (attrSet.getAttr(name).isPresent()) {
    //         return attrSet.getAttr(name).get().getElement();
    //     }
    //     return null;
    // }
    // /**
    //  * 获取字符串数组
    //  *
    //  * @param context 上下文
    //  * @param attrSet 属性集
    //  * @param name 属性name
    //  * @return 返回字符串数组
    //  */
    // public static String[] getStringArray(Context context, AttrSet attrSet, String name) {
    //     String str = getString(attrSet, name, "");
    //     if (str.startsWith("$strarray:")) {
    //         try {
    //             String name2 = str.split(":")[1];
    //             Class<?> aClass = context.getClassloader().loadClass(context.getBundleName() + ".ResourceTable");
    //             Field field = aClass.getField("Strarray_" + name2);
    //             if (field != null) {
    //                 int id = field.getInt("Strarray_" + name2);
    //                 return context.getResourceManager().getElement(id).getStringArray();
    //             }
    //         } catch (NoSuchFieldException | IllegalAccessException | NotExistException | WrongTypeException | IOException | ClassNotFoundException e) {
    //             e.printStackTrace();
    //         }
    //     }
    //     return null;
    // }

    /**
     * 获取资源Id
     *
     * @param attrSet  属性集
     * @param name     属性name
     * @param defValue 默认值
     * @return 返回资源Id
     */
    public static int getResourceId(AttrSet attrSet, String name, int defValue) {
        if (!attrSet.getAttr(name).isPresent()) {
            return defValue;
        }
        String value = attrSet.getAttr(name).get().getStringValue();
        if (value.startsWith("$layout")) {
            try {
                return Integer.parseInt(value.substring(value.indexOf(":") + 1));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return defValue;
    }

}
